package models;
/**
 * 
 */

/**
 * @author Micha�
 *
 */

import java.util.ArrayList;
import java.util.List;

public abstract class Player implements Comparable<Player>
{
	
	Table gameTable;
	List<Card> playerCards;
	public String playerName;
	int tokens;
	int victories = 0;
	Boolean enabled = true;
	Boolean folded = false;
	Boolean left = false;
	int index;
	
	public Player(Table t, String name, int to , int i) {
		this.gameTable = t;
		playerCards = new ArrayList<Card>();
		tokens = to;
		index = i;
		this.playerName = name;
	}
	
	//informuje obiekt, �e teraz jego tura
	public abstract void yourTurn(byte ability, int min);

	//konczy ture gracza
	public void endTurn()
	{
		if(gameTable.whoseTurn() == this)
		{
			gameTable.endTurn(index);
		}
	}
	
	public void fold()
	{
		folded = true;
	}
	
	public String getName()
	{
		return playerName;
	}
	
	public int getTokens()
	{
		return tokens;
	}
	
	public int getVictories()
	{
		return victories;
	}
	
	public void betTokens(int t)
	{
		tokens -= t;
	}
	
	public void win()
	{
		victories++;
	}

	
	public void leave()
	{
		left = true;
	}
	
	public void addTokens(int t)
	{
		tokens +=t;
	}
	
	public void reset()
	{
		playerCards.clear();
		folded = false;
	}
	
	public abstract boolean isHuman();

	public abstract boolean isBot();	
	
	public void addCards(Card c1, Card c2, Card c3, Card c4, Card c5)
	{
		if(playerCards.size() !=0 ) reset();
		playerCards.add(c1);
		playerCards.add(c2);
		playerCards.add(c3);
		playerCards.add(c4);
		playerCards.add(c5);
	}
	
	public void addCard(Card c)
	{
		playerCards.add(c);
	}
	
	public Boolean hasFolded()
	{
		return folded;
	}
	
	public void discardCard(Card c) throws PokerException
	{
		if(!playerCards.contains(c)) throw new PokerException("Nie mo�na wymienic karty - gracz jej nie ma");
		playerCards.remove(c);
	}
	
	public void move()
	{
		gameTable.move(index);
	}
	
	public void move(List<Card> cards) throws PokerException
	{
		int i;
		//wyjatek jak za duzo
		if(cards.size()>4) throw new PokerException("Za duzo kart do wymiany");
		
		int which[] = new int[5];
		for(i=0;i<cards.size();i++)
		{
			int j;
			j = playerCards.indexOf(cards.get(i));
			which[j] = 1;
		}
		gameTable.move(index,which);
		
		//showCards();
	}
	
	public void move(byte movement, int bet)
	{
		gameTable.move(index,movement, bet);
	}
	
	public void showCards()
	{
		int i= 0;
		for(i = 0;i<5;i++) 
		{
			System.out.print(playerCards.get(i).getSuit());
			System.out.print("#");
			System.out.print(playerCards.get(i).getFigure());
			System.out.print(" ");
		}
		System.out.println();
	}
	
	public List<Card> getCards()
	{
		return playerCards;
	}
	
	public int compareTo(Player p)
	{
		if(tokens != p.getTokens()) return tokens - p.getTokens();
		else return victories - p.getVictories();
	}

}
